const entity = require("./entity");
const fs = require("fs");
const path = require("path");
let conowPath = require('./moduleHelper').conowPath;
let entityFilePath = path.join(__dirname,'../templ/daoImpl.temp');
let outputPath = path.join(__dirname,'./output');
//读取文件
let files = fs.readFileSync(entityFilePath,{encoding:'utf-8'});

let entityPath = path.join(conowPath,'./dao');

const templRender = new require('../tools/templRenderUtil');

if(!fs.existsSync(entityPath)){
	fs.mkdirSync(entityPath);
}




exports.ramdom = function(entity){
	let entityUpName = entity.name.charAt(0).toUpperCase()+entity.name.slice(1);
	let entitySubName = entity.name.charAt(0).toLowerCase()+entity.name.slice(1);
	let outputData = files.replace(/\${entity.package}/g,`cn.conow.${entity.module}`);
	outputData = outputData.replace(/\${entity.upName}/g,entityUpName);
	outputData = outputData.replace(/\${entity.tableName}/g,entity.tableName);
	outputData = outputData.replace(/\${entity.author}/g,entity.author);
	outputData = outputData.replace(/\${entity.version}/g,entity.version);
	outputData = outputData.replace(/\${entity.subName}/g,entitySubName);


	outputData = outputData.replace(/\${addExpressions}/g,(data)=>{
		let str = "";
		for(let c in entity.column){
			str += ` 
			db.addCondition(Expression.eq("${c}",${entitySubName}.get${c}()));  `;
		}
		return str;
	});
	outputData = templRender.renderTempl(outputData,entity);


	let fileOutputPath = path.join(entityPath,`./${entityUpName}DaoImpl.java`);
	//fs.writeFileSync(fileOutputPath, outputData);
	return {fileName:`${entityUpName}DaoImpl.java`,data:outputData};	
}